/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman.business.io;


import com.google.common.base.Strings;
import javafx.collections.ObservableMap;
import langman.Utils;
import langman.business.data.TranslationException;
import langman.business.data.TranslationService;
import langman.business.data.model.Category;
import langman.business.data.model.Key;
import langman.business.data.model.Language;
import langman.business.profiles.Profile;
import langman.business.profiles.ProfileService;
import langman.util.doping.Inject;
import langman.util.doping.SingletonBean;
import langman.util.languagefile.commons.TranslationMode;
import langman.util.languagefile.writer.LanguageWriter;

import java.io.File;
import java.io.IOException;

@SingletonBean
public class DataWriter {
    private final TranslationService translationService;
    private final ProfileService profileService;

    @Inject
    public DataWriter(TranslationService translationService, ProfileService profileService) {
        this.translationService = translationService;
        this.profileService = profileService;
    }

    public void writeLanguage(Language language, Iterable<Category> categories, WriteMode writeMode) throws TranslationException {
        Profile selectedProfile = profileService.getSelectedProfile();
        File rootFolder = selectedProfile.getRootFolder();

        String fileName = language.getFileName();
        File tempFile = new File(rootFolder, fileName + "_temp");

        try (LanguageWriter writer = new LanguageWriter(tempFile)) {
            for (Category category : categories) {
                writeCategory(writer, language, category, writeMode);
            }
        } catch (IOException e) {
            throw new TranslationException(e);
        }

        File realFile = new File(rootFolder, fileName);
        try {
            Utils.overwriteWithTempFile(tempFile, realFile);
        } catch (IOException e) {
            throw new TranslationException(e);
        }
    }


    private void writeCategory(LanguageWriter writer, Language language, Category category, WriteMode writeMode) throws IOException {
        ObservableMap<String, Key> keyMap = category.getKeysUnmodifiable();
        
        if (!keyMap.isEmpty()) {
            writer.writeCategory(category.getName());
        }

        Language referenceLanguage = translationService.getReferenceLanguage();
        
        for (String keyName : keyMap.keySet()) {
            Key key = keyMap.get(keyName);

            String translation = key.getTranslation(language);

            boolean asReference = Utils.equalsIgnoreCase(key.getTranslation(referenceLanguage), translation);

            if (translation == null && writeMode == WriteMode.jBEAM) {
                translation = key.getTranslation(referenceLanguage);
            }

            TranslationMode translationMode = determineTranslationMode(key, asReference);

            if (!Strings.isNullOrEmpty(translation)) {
                writer.writeTranslation(key.getKey(), translation, translationMode);
            }
        }
    }

    private TranslationMode determineTranslationMode(Key key, boolean asReference) {
        TranslationMode translationMode = null;
        if (asReference) {
            translationMode = TranslationMode.AsReference;
        }
        if (key.isDontTranslate()) {
            translationMode = TranslationMode.DontTranslate;
        }
        return translationMode;
    }
}